﻿function ActionTrackMgmt() {

    this.SearchCtrl = null;
    this.ModuleActionsCtrl = null;

    this.IDCtrl = null;
    this.CodeCtrl = null;
    this.TitleCtrl = null;
    this.DescriptionCtrl = null;
    this.NCDescriptionCtrl = null;
    this.TypeCtrl = null;
    this.ResponderCtrl = null;
    this.ExpectedDateCtrl = null;
    this.ReviewersCtrl = null;
    this.SourceCtrl = null;
    this.PriorityCtrl = null;
    this.StatusCtrl = null;
    this.TabCtrl = null;
    this.VesselCtrl = null;
    this.DepartmentCtrl = null;
    this.FileUploader = null;

    //Response Object
    this.ResponderNameCtrl = null;
    this.ResponseDate = null;
    this.ResponseCtrl = null;
    this.ResponseFileUploader = null;

    this.ReviewerNameCtrl = null;
    this.ReviewDate = null;
    this.ReviewCtrl = null;
    this.ReviewStatusCtrl = null;

    this.HseqNameCtrl = null;
    this.HseqDate = null;
    this.HsqeCommentsCtrl = null;
    this.HseqApprovalStatusCtrl = null;

    this.OdNameCtrl = null;
    this.OdDate = null;
    this.OdCommentsCtrl = null;
    this.OdApprovalStatusCtrl = null;

    this.ResponseObjectCtrl = null;
    this.ActionCtrl = null;

    this.OriginalResponseCtrl = null;
    this.ShowHistoryBtnCtrl = null;

    //Search Control
    this.AdvSearchCodeCtrl = null;
    this.AdvSearchTypeCtrl = null;
    this.AdvSearchSourceCtrl = null;
    this.AdvSearchPriorityCtrl = null;
    this.AdvSearchAssignedToCtrl = null;
    this.AdvSearchStatusCtrl = null;
    this.AdvSearchExpectedDateFromCtrl = null;
    this.AdvSearchExpectedDateToCtrl = null;
    this.AdvSearchFinalizeDateFromCtrl = null;
    this.AdvSearchFinalizeDateToCtrl = null;

    this.GetActiveTabIndex = function () {
        if (this.TabCtrl != null) {
            return this.TabCtrl.get_selectedTab().get_index();
        }
        else {
            throw 'Cannot load RadTabStrip.';
        }
    }

    this.SelectTabByName = function (pName) {
        if (this.TabCtrl != null) {
            return this.TabCtrl.findTabByText(pName).set_selected(true);
        }
        else {
            throw 'Cannot load Tabs.';
        }
    }

    this.SelectTabByIndex = function (pTabIdx) {
        if (this.TabCtrl != null) {
            this.TabCtrl.get_tabs().getTab(pTabIdx).select();
        }
        else {
            throw 'Cannot load Tabs.';
        }
    }

    this.SaveCAR = function (pUserCode) {
        if (this.CheckUserInputForDetails(false)) {
            var carObject = MASTER_GetCurrentBusinessObject(); 

            if (carObject == null) {
                carObject = new Landscape.Action();
                carObject.ID = '00000000-0000-0000-0000-000000000000';
                carObject.CreatedBy = pUserCode;
                carObject.CreatedDate = new Date();
                carObject.IsDeleted = false;
                carObject.IsReleased = false;
                carObject.Status = 'NEW';
            }
            
            if (carObject.IsReleased != true) {
                carObject.Title = this.TitleCtrl.get_value();
                carObject.Description = this.DescriptionCtrl.get_value();
                carObject.NonConforDescription = this.NCDescriptionCtrl.get_value();
                carObject.AssignedTo = (this.ResponderCtrl.get_value().trim() == '' ? '00000000-0000-0000-0000-000000000000' : this.ResponderCtrl.get_value().trim());
                carObject.ResponseDueDate = this.ExpectedDateCtrl.get_selectedDate();
                carObject.Priority = this.PriorityCtrl.get_value();
                carObject.Type = this.TypeCtrl.get_value();
                carObject.Source = this.SourceCtrl.get_value();
                carObject.UpdatedBy = pUserCode;
                carObject.UpdatedDate = new Date();
                carObject.VesselCode = this.VesselCtrl.get_value();
                carObject.DepartmentCode = this.DepartmentCtrl.get_value();

                carObject.Reviewers = new Array();
                carObject.Reviewers.push(this.ReviewersCtrl.get_value());

                Landscape.OperationsActions.SaveAction(carObject, function (result, args) {
                    args.bizObject.ID = result;
                    MASTER_StoreCurrentBusinessObject(args.bizObject);
                    N_NotifyMessage('Saved', 'The active improvement action details saved successfully.', 1);

                    var files = args.jsObject.FileUploader.GetUploadedFiles();
                    Landscape.OperationsActions.CommitActionFiles(files, result);

                }, function (result, args) { N_NotifyMessage('Error', result.get_message(), 2); }, { jsObject: this, bizObject: carObject });
            }
            else {
                N_NotifyMessage('Invalid', 'The action details already finalized. Cannot be finalized again.', 2);
            }
        }
    }

    this.ReleaseCAR = function () {
        N_ConfirmMessage('Confirm', 'Releasing the action detail will initiate the workflow. You cannot edit any of the data hereafter. Do you want to proceed?', 3, this.ContinueReleaseCAR, null, this);
    }

    this.ContinueReleaseCAR = function (params) {
        if (params.CheckUserInputForDetails(true)) {
            var carObject = MASTER_GetCurrentBusinessObject();
            if (carObject == null || carObject == '') {
                carObject = new Landscape.Action();
                carObject.ID = '00000000-0000-0000-0000-000000000000';
                carObject.CreatedBy = MASTER_GetCurrentUserCode();
                carObject.CreatedDate = new Date();
                carObject.IsDeleted = false;

                carObject.Status = 'NEW';
            }
            else {
                //carObject.ID = this.IDCtrl.value.trim()
                carObject.IsDeleted = false;
            }

            if (!carObject.IsReleased) {
                carObject.Title = params.TitleCtrl.get_value();
                carObject.Description = params.DescriptionCtrl.get_value();
                carObject.NonConforDescription = params.NCDescriptionCtrl.get_value();
                carObject.AssignedTo = (params.ResponderCtrl.get_value().trim() == '' ? '00000000-0000-0000-0000-000000000000' : params.ResponderCtrl.get_value().trim());
                carObject.ResponseDueDate = params.ExpectedDateCtrl.get_selectedDate();
                carObject.Priority = params.PriorityCtrl.get_value();
                carObject.Type = params.TypeCtrl.get_value();
                carObject.Source = params.SourceCtrl.get_value();
                carObject.UpdatedBy = MASTER_GetCurrentUserCode();
                carObject.UpdatedDate = new Date();
                carObject.IsReleased = true;
                carObject.ReleaseBy = MASTER_GetCurrentUserCode();
                carObject.ReleasedDate = new Date();
                carObject.VesselCode = params.VesselCtrl.get_value();
                carObject.DepartmentCode = params.DepartmentCtrl.get_value();

                Landscape.OperationsActions.SaveAction(carObject, function (result, args) {
                    N_NotifyMessage('Released', 'The active action details finalized successfully.', 1);
                    MASTER_StoreCurrentBusinessObject(args.bizObject);
                    var files = args.jsObject.FileUploader.GetUploadedFiles();
                    Landscape.OperationsActions.CommitActionFiles(files, result);
                }, function (result, args) { N_NotifyMessage('Error', result.get_message(), 2); }, { jsObject: params, bizObject: carObject });
            }
            else {
                N_NotifyMessage('Invalid', 'The action details already finalized. Cannot be finalized again.', 2);
            }
        }
    }

    this.CheckUserInputForDetails = function (forRelease) {

        var errors = "";
        var hasErrors = false;

        //Title
        if (this.TitleCtrl != null) {
            if (forRelease) {
                if (this.TitleCtrl.get_value().trim() == '') {
                    errors += '<li>Title for this action is missing.</li>';
                    hasErrors = true;
                }
            }

            if (this.TitleCtrl.get_value().length >= 255) {
                errors += '<li>Length of the title is exceeding 256 charectors.</li>';
                hasErrors = true;
            }

            if (this.TitleCtrl.get_value().trim().length == 0) {
                errors += '<li>Enter the titel for this action to save.</li>';
                hasErrors = true;
            }
        }
        else {
            errors += '<li>Cannot instantiate title control.</li>';
            hasErrors = true;
        }

        //Discription
        if (this.DescriptionCtrl != null) {
            if (forRelease) {
                if (this.DescriptionCtrl.get_value().trim() == '') {
                    errors += '<li>Action description is missing.</li>';
                    hasErrors = true;
                }
            }

            if (this.DescriptionCtrl.get_value().length >= 4000) {
                errors += '<li>Length of the description is exceeding 4000 charectors.</li>';
                hasErrors = true;
            }
        }
        else {
            errors += '<li>Cannot instantiate description control.</li>';
            hasErrors = true;
        }

        //Action Type
        if (this.TypeCtrl != null) {
            if (forRelease) {
                if (this.TypeCtrl.get_value().trim() == '-Select-') {
                    errors += '<li>Select action type.</li>';
                    hasErrors = true;
                }
            }
        }
        else {
            errors += '<li>Cannot instantiate action type control.</li>';
            hasErrors = true;
        }

        //Action Source
        if (this.SourceCtrl != null) {
            if (forRelease) {
                if (this.SourceCtrl.get_value().trim() == '-Select-') {
                    errors += '<li>Select action source.</li>';
                    hasErrors = true;
                }
            }
        }
        else {
            errors += '<li>Cannot instantiate action source control.</li>';
            hasErrors = true;
        }

        //Action Priority
        if (this.PriorityCtrl != null) {
            if (forRelease) {
                if (this.PriorityCtrl.get_value().trim() == '-Select-') {
                    errors += '<li>Select action priority.</li>';
                    hasErrors = true;
                }
            }
        }
        else {
            errors += '<li>Cannot instantiate action priority control.</li>';
            hasErrors = true;
        }

        //Responder
        if (this.ResponderCtrl != null) {
            if (forRelease) {
                if (this.ResponderCtrl.get_value().trim() == '00000000-0000-0000-0000-000000000000') {
                    errors += '<li>Select action responder.</li>';
                    hasErrors = true;
                }
            }
        }
        else {
            errors += '<li>Cannot instantiate action responder control.</li>';
            hasErrors = true;
        }

        //Reviewer
        if (this.ReviewersCtrl != null) {
            if (forRelease) {
                if (this.ReviewersCtrl.get_value().trim() == '00000000-0000-0000-0000-000000000000') {
                    errors += '<li>Select action reviewer.</li>';
                    hasErrors = true;
                }
            }
        }
        else {
            errors += '<li>Cannot instantiate action reviewer control.</li>';
            hasErrors = true;
        }

        //Expected date
        if (this.ExpectedDateCtrl != null) {
            if (forRelease) {
                if (this.ExpectedDateCtrl.get_selectedDate() <= new Date()) {
                    errors += '<li>Expected date should be later today.</li>';
                    hasErrors = true;
                }
            }

            if (this.PriorityCtrl.get_value().trim() == 'HIGH' && this.ExpectedDateCtrl.get_selectedDate() > (new Date().setDate(new Date().getDate() + 30))) {
                errors += '<li>The expected date cannot be more then 30 days from today for high priority actions.</li>';
                hasErrors = true;
            }
        }
        else {
            errors += '<li>Cannot instantiate action reviewer control.</li>';
            hasErrors = true;
        }

        if (hasErrors) {
            N_NotifyMessage('Errors', String.format('Please check the following <ul class="SmallFont">{0}</ul>', errors), 2);
        }

        return !hasErrors;
    }

    this.CheckUserInputForResponse = function (forRelease) {
        var action = this.ActionCtrl.value.toLowerCase();
        var hasErrors = false;
        var errors = '';

        switch (action) {
            case 'rework':
            case 'respond':
                {
                    if (this.ResponseCtrl.get_value().trim() == '') {
                        errors += '<li>Response should not be empty.</li>';
                        hasErrors = true;
                    }
                    else {
                        if (this.ResponseCtrl.get_value().trim().length > 4000) {
                            errors += '<li>Response should not exceed 4000 charectors.</li>';
                            hasErrors = true;
                        }
                    }
                    break;
                }

            case 'review':
                {
                    if (this.ReviewCtrl.get_value().trim() == '') {
                        errors += '<li>Review should not be empty.</li>';
                        hasErrors = true;
                    }
                    else {
                        if (this.ReviewCtrl.get_value().trim().length > 4000) {
                            errors += '<li>Review should not exceed 4000 charectors.</li>';
                            hasErrors = true;
                        }
                    }
                    break;
                }

            case 'hseqapproval':
                {
                    if (this.HsqeCommentsCtrl.get_value().trim() == '') {
                        errors += '<li>Comments should not be empty.</li>';
                        hasErrors = true;
                    }
                    else {
                        if (this.HsqeCommentsCtrl.get_value().trim().length > 4000) {
                            errors += '<li>Comments should not exceed 4000 charectors.</li>';
                            hasErrors = true;
                        }
                    }
                    break;
                }

            case 'odapproval':
                {
                    if (this.OdCommentsCtrl.get_value().trim() == '') {
                        errors += '<li>Comments should not be empty.</li>';
                        hasErrors = true;
                    }
                    else {
                        if (this.OdCommentsCtrl.get_value().trim().length > 4000) {
                            errors += '<li>Comments should not exceed 4000 charectors.</li>';
                            hasErrors = true;
                        }
                    }
                    break;
                }
        }

        if (hasErrors) {
            N_NotifyMessage('Errors', String.format('Please check the following <ul class="SmallFont">{0}</ul>', errors), 2);
        }

        return !hasErrors;
    }

    this.LoadAllUsers = function (rcbSender) {
        if (rcbSender != null) this.AssignedTo = rcbSender;
        Landscape.AdministrativeActions.GetUsersByPage(0, -1, function (result, e) {
            e.AssignedTo.clearItems();
            var items = e.AssignedTo.get_items();
            e.AssignedTo.trackChanges();

            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text('-Select-');
            comboItem.set_value('00000000-0000-0000-0000-000000000000');
            items.add(comboItem);

            for (var idx = 0; idx < result.length; idx++) {
                comboItem = new Telerik.Web.UI.RadComboBoxItem();
                comboItem.set_text(result[idx].FirstName + ' ' + result[idx].LastName);
                comboItem.set_value(result[idx].ID);

                items.add(comboItem);
            }

            items.getItem(0).select();

            e.AssignedTo.commitChanges();
        }, function (result, e) { }, this);
    }

    this.LoadReviewers = function (sourceType) {
        sourceType = "MM";
        //Landscape.OperationsActions.GetReviewers(sourceType, this.VesselCtrl.get_value(), this.DepartmentCtrl.get_value(), function (result, e) {
        Landscape.OperationsActions.GetReviewers(sourceType, null, null, function (result, e) {
            e.ReviewersCtrl.clearItems();
            var items = e.ReviewersCtrl.get_items();
            e.ReviewersCtrl.trackChanges();

            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text('-Select-');
            comboItem.set_value('00000000-0000-0000-0000-000000000000');
            items.add(comboItem);

            for (var idx = 0; idx < result.length; idx++) {
                comboItem = new Telerik.Web.UI.RadComboBoxItem();
                comboItem.set_text(result[idx].FirstName + ' ' + result[idx].LastName);
                comboItem.set_value(result[idx].ID);

                items.add(comboItem);
            }

            items.getItem(0).select();

            e.ReviewersCtrl.commitChanges();
        }, function (result, e) { alert(result.get_message()); }, this);
    }

    this.LoadUserVessels = function (pUserCode) {
        Landscape.AdministrativeActions.GetUserVessels(pUserCode, function (result, e) {
            e.VesselCtrl.clearItems();
            var items = e.VesselCtrl.get_items();
            e.VesselCtrl.trackChanges();

            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text('-Select-');
            comboItem.set_value('00000000-0000-0000-0000-000000000000');
            items.add(comboItem);

            for (var idx = 0; idx < result.length; idx++) {
                comboItem = new Telerik.Web.UI.RadComboBoxItem();
                comboItem.set_text(result[idx].Name);
                comboItem.set_value(result[idx].ID);

                items.add(comboItem);
            }

            items.getItem(0).select();

            e.VesselCtrl.commitChanges();
        }, function (result, e) { alert(result.get_message()); }, this);
    }

    this.LoadUserDepartments = function (pUserCode) {
        Landscape.AdministrativeActions.GetUserDepartments(pUserCode, function (result, e) {
            e.DepartmentCtrl.clearItems();
            var items = e.DepartmentCtrl.get_items();
            e.DepartmentCtrl.trackChanges();

            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text('-Select-');
            comboItem.set_value('00000000-0000-0000-0000-000000000000');
            items.add(comboItem);

            for (var idx = 0; idx < result.length; idx++) {
                comboItem = new Telerik.Web.UI.RadComboBoxItem();
                comboItem.set_text(result[idx].Name);
                comboItem.set_value(result[idx].ID);

                items.add(comboItem);
            }

            items.getItem(0).select();

            e.DepartmentCtrl.commitChanges();
        }, function (result, e) { alert(result.get_message()); }, this);
    }

    this.LoadActionObject = function (pActionID) {
        Landscape.OperationsActions.GetAction(pActionID, function (result, e) {            
            MASTER_StoreCurrentBusinessObject(result);
            e.LoadActionDetails();
        }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, this);
    }

    this.LoadActionDetails = function () {
        var bizObject = MASTER_GetCurrentBusinessObject();
        this.IDCtrl.value = bizObject.ID;
        this.TitleCtrl.set_value(bizObject.Title);
        this.DescriptionCtrl.set_value(bizObject.Description);
        this.NCDescriptionCtrl.set_value(bizObject.NonConforDescription);
        this.ExpectedDateCtrl.set_selectedDate(bizObject.ResponseDueDate);
        this.CodeCtrl.innerHTML = bizObject.Code;
        this.StatusCtrl.innerHTML = bizObject.Status;

        this.FileUploader.LoadFiles(MASTER_GetCurrentBusinessObject().ID);
        this.FileUploader.ClearFilesToUpload();

        if (this.ResponderCtrl.findItemByValue(bizObject.AssignedTo)) this.ResponderCtrl.findItemByValue(bizObject.AssignedTo).select();
        if (this.VesselCtrl.findItemByValue(bizObject.VesselCode) != null) this.VesselCtrl.findItemByValue(bizObject.VesselCode).select();
        if (this.DepartmentCtrl.findItemByValue(bizObject.DepartmentCode) != null) this.DepartmentCtrl.findItemByValue(bizObject.DepartmentCode).select();
        if (this.TypeCtrl.findItemByValue(bizObject.Type) != null) this.TypeCtrl.findItemByValue(bizObject.Type).select();
        if (this.ReviewersCtrl.findItemByValue(bizObject.Reviewers[0]) != null) this.ReviewersCtrl.findItemByValue(bizObject.Reviewers[0]).select();
        if (this.SourceCtrl.findItemByValue(bizObject.Source) != null) this.SourceCtrl.findItemByValue(bizObject.Source).select();
        if (this.PriorityCtrl.findItemByValue(bizObject.Priority) != null) this.PriorityCtrl.findItemByValue(bizObject.Priority).select();

        Landscape.OperationsActions.HasResponseHistory(bizObject.ID, function (result, e) {
            e.ShowHistoryBtnCtrl.set_visible(result);
        }, function (result, e) { e.ShowHistoryBtnCtrl.set_visible(false); }, this);

        Landscape.OperationsActions.GetActiveResponse(bizObject.ID, function (result, e) {
            if (result != null) {
                var action = e.ActionCtrl.value.toLowerCase();

                if (action == 'rework') {
                    GLOBAL_SetetBusinessObjectTo(e.ResponseObjectCtrl, null);
                    e.OriginalResponseCtrl.innerHTML = result.Response;
                    if ($('#ortr') != null)
                        $('#ortr').show();
                }
                else {

                    GLOBAL_SetetBusinessObjectTo(e.ResponseObjectCtrl, result);
                    e.ResponseCtrl.set_value(result.Response);
                    if ($('#ortr') != null)
                        $('#ortr').hide();
                }
                e.ReviewCtrl.set_value(result.ReviewerComments);

                if (result.Response.trim() != '') {
                    e.ResponderNameCtrl.innerHTML = result.ResponseUser;
                    e.ResponseDate.innerHTML = result.ResponseDate;
                }

                e.ResponseFileUploader.LoadFiles(result.ID);
                e.ResponseFileUploader.ClearFilesToUpload();

                if (result.ReviewStatus != null && result.ReviewStatus != '') {
                    (result.ReviewStatus == 'APPROVED' ? e.ReviewStatusCtrl.rows[0].cells[0].firstChild.checked = true : e.ReviewStatusCtrl.rows[0].cells[1].firstChild.checked = true);
                    e.ReviewerNameCtrl.innerHTML = result.ReviewCommentsUser;
                    e.ReviewDate.innerHTML = result.ReviewDate;
                }

                e.HsqeCommentsCtrl.set_value(result.HseqComments);

                if (result.HseqApprovalStatus != null && result.HseqApprovalStatus != '') {
                    (result.HseqApprovalStatus == 'APPROVED' ? e.HseqApprovalStatusCtrl.rows[0].cells[0].firstChild.checked = true : e.HseqApprovalStatusCtrl.rows[0].cells[1].firstChild.checked = true);
                    e.HseqNameCtrl.innerHTML = result.HseqCommentsUser;
                    e.HseqDate.innerHTML = result.HseqApprovalDate;
                }

                e.OdCommentsCtrl.set_value(result.OdComments);

                if (result.OdApprovalStatus != null && result.OdApprovalStatus != '') {
                    (result.OdApprovalStatus == 'APPROVED' ? e.OdApprovalStatusCtrl.rows[0].cells[0].firstChild.checked = true : e.OdApprovalStatusCtrl.rows[0].cells[1].firstChild.checked = true);
                    this.OdNameCtrl.innerHTML = result.OdCommentsUser;
                    this.OdDate.innerHTML = result.OdApprovalDate;
                }
            }
        }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, this);
    }

    this.SaveResponse = function (pResponseObject) {
        var respObject = null;
        var actionID = MASTER_GetCurrentBusinessObject().ID;
        action = this.ActionCtrl.value.toLowerCase();

        if (this.CheckUserInputForResponse(false)) {
            switch (action.toLowerCase()) {
                case 'rework':
                case 'respond':
                    {
                        if (pResponseObject == null) {
                            //New Response
                            respObject = new Landscape.ActionResponse();
                            respObject.ID = '00000000-0000-0000-0000-000000000000';
                            respObject.CreatedBy = MASTER_GetCurrentUserCode();
                            respObject.CreatedDate = new Date();
                            respObject.Status = 'NEW';
                        }
                        else {
                            //Existing
                            respObject = pResponseObject;
                        }

                        if (!respObject.IsResponseReleased) {
                            respObject.Response = this.ResponseCtrl.get_value();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.ResponseBy = MASTER_GetCurrentUserCode();
                            respObject.ResponseDate = new Date();

                            Landscape.OperationsActions.SaveResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Saved', 'The active response saved successfully.', 1);

                                var files = e.jsObject.ResponseFileUploader.GetUploadedFiles();
                                Landscape.OperationsActions.CommitActionResponseFiles(files, MASTER_GetCurrentBusinessObject().ID, result);
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active response is already released. Cannot be saved.', 2);
                        }

                        break;
                    }

                case 'review':
                    {
                        respObject = pResponseObject;

                        if (!respObject.IsReviewReleased) {
                            respObject.ReviewerComments = this.ReviewCtrl.get_value();
                            respObject.ReviewStatus = (this.ReviewStatusCtrl.rows[0].cells[0].firstChild.checked == true ? 'APPROVED' : this.ReviewStatusCtrl.rows[0].cells[1].firstChild.checked == true ? 'REWORK' : '');
                            respObject.ReviewDate = new Date();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.ReviewCommentsBy = MASTER_GetCurrentUserCode();

                            Landscape.OperationsActions.SaveResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Saved', 'The active review details saved successfully.', 1);
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active review is already released. Cannot be saved.', 2);
                        }
                        break;
                    }
                case 'hseqapproval':
                    {
                        respObject = pResponseObject;

                        if (!respObject.IsHseqApprovalReleased) {
                            respObject.HseqComments = this.HsqeCommentsCtrl.get_value();
                            respObject.HseqApprovalStatus = (this.HseqApprovalStatusCtrl.rows[0].cells[0].firstChild.checked == true ? 'APPROVED' : this.HseqApprovalStatusCtrl.rows[0].cells[1].firstChild.checked == true ? 'REWORK' : '');
                            respObject.HseqApprovalDate = new Date();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.HseqCommentsBy = MASTER_GetCurrentUserCode();

                            Landscape.OperationsActions.SaveResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Saved', 'The active HSEQ approval details saved successfully.', 1);
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active HEQ response is already released. Cannot be saved.', 2);
                        }
                        break;
                    }
                case 'odapproval':
                    {
                        respObject = pResponseObject;

                        if (!respObject.IsOdApprovalReleased) {
                            respObject.OdComments = this.OdCommentsCtrl.get_value();
                            respObject.OdApprovalStatus = (this.OdApprovalStatusCtrl.rows[0].cells[0].firstChild.checked == true ? 'APPROVED' : this.OdApprovalStatusCtrl.rows[0].cells[1].firstChild.checked == true ? 'REWORK' : '');
                            respObject.OdApprovalDate = new Date();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.OdCommentsBy = MASTER_GetCurrentUserCode();

                            Landscape.OperationsActions.SaveResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Saved', 'The active Operation Director approval details saved successfully.', 1);
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active OD response is already released. Cannot be saved.', 2);
                        }
                        break;
                    }
                default:
                    {
                        N_NotifyMessage('Invalid Action', 'There is no action pending from you. This might be due to the workflow has not been initiated or you are not useing the link provided in the email sent by the system.', 3);
                        break;
                    }
            }
        }
    }

    this.ReleaseResponse = function (pResponseObject) {
        var respObject = null;
        var actionID = MASTER_GetCurrentBusinessObject().ID;
        action = this.ActionCtrl.value.toLowerCase();

        if (this.CheckUserInputForResponse(true)) {
            switch (action) {
                case 'rework':
                case 'respond':
                    {
                        if (pResponseObject == null) {
                            //New Response
                            respObject = new Landscape.ActionResponse();
                            respObject.ID = '00000000-0000-0000-0000-000000000000';
                            respObject.CreatedBy = MASTER_GetCurrentUserCode();
                            respObject.CreatedDate = new Date();
                            respObject.Status = 'NEW';
                        }
                        else {
                            //Existing
                            respObject = pResponseObject;
                        }

                        if (!respObject.IsResponseReleased) {
                            respObject.Response = this.ResponseCtrl.get_value();
                            respObject.ResponseBy = MASTER_GetCurrentUserCode();
                            respObject.ResponseDate = new Date();
                            respObject.Status = 'RELEASED';
                            respObject.IsResponseReleased = true;
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();

                            Landscape.OperationsActions.ReleaseResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Finalized', 'The active response finalized successfully.', 1);

                                var files = e.jsObject.ResponseFileUploader.GetUploadedFiles();
                                Landscape.OperationsActions.CommitActionResponseFiles(files, MASTER_GetCurrentBusinessObject().ID, result);

                                e.jsObject.ActionCtrl.value = '';
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active response is already released. Cannot be saved.', 2);
                        }

                        break;
                    }
                case 'review':
                    {
                        respObject = pResponseObject;

                        if (!respObject.IsReviewReleased) {
                            respObject.ReviewerComments = this.ReviewCtrl.get_value();
                            respObject.ReviewStatus = (this.ReviewStatusCtrl.rows[0].cells[0].firstChild.checked == true ? 'APPROVED' : this.ReviewStatusCtrl.rows[0].cells[1].firstChild.checked == true ? 'REWORK' : '');
                            respObject.Status = respObject.ReviewStatus;
                            respObject.ReviewDate = new Date();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.ReviewCommentsBy = MASTER_GetCurrentUserCode();
                            respObject.IsReviewReleased = true;

                            Landscape.OperationsActions.ReleaseResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Finalize', 'The active review details finilized successfully.', 1);
                                e.jsObject.ActionCtrl.value = '';
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active review is already released. Cannot be saved.', 2);
                        }
                        break;
                    }
                case 'hseqapproval':
                    {
                        respObject = pResponseObject;

                        if (!respObject.IsHseqApprovalReleased) {
                            respObject.HseqComments = this.HsqeCommentsCtrl.get_value();
                            respObject.HseqApprovalStatus = (this.HseqApprovalStatusCtrl.rows[0].cells[0].firstChild.checked == true ? 'APPROVED' : this.HseqApprovalStatusCtrl.rows[0].cells[1].firstChild.checked == true ? 'REWORK' : '');
                            respObject.HseqApprovalDate = new Date();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.HseqCommentsBy = MASTER_GetCurrentUserCode();
                            respObject.IsHseqApprovalReleased = true;

                            Landscape.OperationsActions.ReleaseResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Finalize', 'The active HSEQ approval details finalized successfully.', 1);
                                e.jsObject.ActionCtrl.value = '';
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active HEQ response is already released. Cannot be saved.', 2);
                        }
                        break;
                    }
                case 'odapproval':
                    {
                        respObject = pResponseObject;

                        if (!respObject.IsOdApprovalReleased) {
                            respObject.OdComments = this.OdCommentsCtrl.get_value();
                            respObject.OdApprovalStatus = (this.OdApprovalStatusCtrl.rows[0].cells[0].firstChild.checked == true ? 'APPROVED' : this.OdApprovalStatusCtrl.rows[0].cells[1].firstChild.checked == true ? 'REWORK' : '');
                            respObject.OdApprovalDate = new Date();
                            respObject.UpdatedBy = MASTER_GetCurrentUserCode();
                            respObject.UpdatedDate = new Date();
                            respObject.OdCommentsBy = MASTER_GetCurrentUserCode();
                            respObject.IsOdApprovalReleased = true;

                            Landscape.OperationsActions.ReleaseResponse(actionID, respObject, action, function (result, e) {
                                e.bizObject.ID = result;
                                GLOBAL_SetetBusinessObjectTo(e.jsObject.ResponseObjectCtrl, e.bizObject);
                                N_NotifyMessage('Finalize', 'The active Operation Director approval details finalized successfully.', 1);
                                e.jsObject.ActionCtrl.value = '';
                            }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, { bizObject: respObject, jsObject: this });
                        }
                        else {
                            N_NotifyMessage('Invalid', 'The active OD response is already released. Cannot be saved.', 2);
                        }

                        break;
                    }
                default:
                    {
                        N_NotifyMessage('Invalid Action', 'There is no action pending from you. This might be due to the workflow has not been initiated or you are not useing the link provided in the email sent by the system.', 3);
                        break;
                    }
            }
        }
    }

    this.ClearResponseCtrl = function () {
        this.ResponseCtrl.set_value('');
        this.ReviewCtrl.set_value('');
        this.HsqeCommentsCtrl.set_value('');
        this.OdCommentsCtrl.set_value('');

        this.ReviewStatusCtrl.rows[0].cells[0].firstChild.checked = false;
        this.ReviewStatusCtrl.rows[0].cells[1].firstChild.checked = false;

        this.HseqApprovalStatusCtrl.rows[0].cells[0].firstChild.checked = false;
        this.HseqApprovalStatusCtrl.rows[0].cells[1].firstChild.checked = false;

        this.OdApprovalStatusCtrl.rows[0].cells[0].firstChild.checked = false;
        this.OdApprovalStatusCtrl.rows[0].cells[1].firstChild.checked = false;

        this.ResponseObjectCtrl.value = '';

        this.ResponderNameCtrl.innerHTML = '';
        this.ResponseDate.innerHTML = '';
        this.ReviewerNameCtrl.innerHTML = '';
        this.ReviewDate.innerHTML = '';
        this.HseqNameCtrl.innerHTML = '';
        this.HseqDate.innerHTML = '';
        this.OdNameCtrl.innerHTML = '';
        this.OdDate.innerHTML = '';

        this.ResponseFileUploader.Reset();

        GLOBAL_SetetBusinessObjectTo(this.ResponseObjectCtrl, null);
    }

    this.ClearDetailsCtrl = function () {
        this.IDCtrl.value = '';
        this.CodeCtrl.innerHTML = '';
        this.TitleCtrl.set_value('');
        this.DescriptionCtrl.set_value('');
        this.NCDescriptionCtrl.set_value('');
        this.TypeCtrl.clearSelection();
        this.TypeCtrl.get_items().getItem(0).select();
        this.ResponderCtrl.clearSelection();
        this.ResponderCtrl.get_items().getItem(0).select();
        this.ReviewersCtrl.clearSelection();
        this.ReviewersCtrl.get_items().getItem(0).select();
        this.SourceCtrl.clearSelection();
        this.SourceCtrl.get_items().getItem(0).select();
        this.PriorityCtrl.clearSelection();
        this.PriorityCtrl.get_items().getItem(0).select();
        this.VesselCtrl.clearSelection();
        this.VesselCtrl.get_items().getItem(0).select();
        this.DepartmentCtrl.clearSelection();
        this.DepartmentCtrl.get_items().getItem(0).select();
        this.FileUploader.Reset();
        this.ExpectedDateCtrl.set_selectedDate(null);
        this.StatusCtrl.innerHTML = '';

        MASTER_StoreCurrentBusinessObject(null);
    }

    this.SearchActions = function () {
        var searchParam = new Array();
        /*for (idx = 0; idx < 10; idx++)
        searchParam[idx] = new Array(2);*/

        //var row = 0;
        if (this.AdvSearchCodeCtrl.get_value().trim() != '') {
            var tapul = new Array(2);
            tapul[0] = 'Code';
            tapul[1] = this.AdvSearchCodeCtrl.get_value().trim();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchTypeCtrl.get_value().trim() != '-Select-' && this.AdvSearchTypeCtrl.get_value().trim() != '') {
            var tapul = new Array(2);
            tapul[0] = 'Type';
            tapul[1] = this.AdvSearchTypeCtrl.get_value().trim();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchSourceCtrl.get_value().trim() != '-Select-' && this.AdvSearchSourceCtrl.get_value().trim() != '') {
            var tapul = new Array(2);
            tapul[0] = 'Source';
            tapul[1] = this.AdvSearchSourceCtrl.get_value().trim();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchPriorityCtrl.get_value().trim() != '-Select-' && this.AdvSearchPriorityCtrl.get_value().trim() != '') {
            var tapul = new Array(2);
            tapul[0] = 'Priority';
            tapul[1] = this.AdvSearchPriorityCtrl.get_value().trim();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchAssignedToCtrl.get_value().trim() != '-Select-' && this.AdvSearchAssignedToCtrl.get_value().trim() != '') {
            var tapul = new Array(2);
            tapul[0] = 'AssignedTo';
            tapul[1] = this.AdvSearchAssignedToCtrl.get_value().trim();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchStatusCtrl.get_value().trim() != '-Select-' && this.AdvSearchStatusCtrl.get_value().trim() != '') {
            var tapul = new Array(2);
            tapul[0] = 'Status';
            tapul[1] = this.AdvSearchStatusCtrl.get_value().trim();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchExpectedDateFromCtrl.get_selectedDate() != null) {
            var tapul = new Array(2);
            tapul[0] = 'ExpectedFrom';
            tapul[1] = this.AdvSearchExpectedDateFromCtrl.get_selectedDate().toDateString();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchExpectedDateToCtrl.get_selectedDate() != null) {
            var tapul = new Array(2);
            tapul[0] = 'ExpectedTo';
            tapul[1] = this.AdvSearchExpectedDateToCtrl.get_selectedDate().toDateString();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchFinalizeDateFromCtrl.get_selectedDate() != null) {
            var tapul = new Array(2);
            tapul[0] = 'FinalizeFrom';
            tapul[1] = this.AdvSearchFinalizeDateFromCtrl.get_selectedDate().toDateString();

            searchParam.push(tapul);
            //row++;
        }

        if (this.AdvSearchFinalizeDateToCtrl.get_selectedDate() != null) {
            var tapul = new Array(2);
            tapul[0] = 'FinalizeTo';
            tapul[1] = this.AdvSearchFinalizeDateToCtrl.get_selectedDate().toDateString();

            searchParam.push(tapul);
            //row++;
        }

        Landscape.OperationsActions.SearchActions(searchParam, this.SearchCtrl.get_masterTableView().get_currentPageIndex(), this.SearchCtrl.get_masterTableView().get_pageSize(), function (result, e) {
            e.SearchCtrl.get_masterTableView().set_dataSource(result);
            e.SearchCtrl.get_masterTableView().dataBind();
        }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, this);

        Landscape.OperationsActions.SearchActionsCount(searchParam, function (result, e) {
            e.SearchCtrl.get_masterTableView().set_virtualItemCount(result);
        }, function (result, e) { N_NotifyMessage('Error', result.get_message(), 2); }, this);
    }

    this.LoadSelectedAction = function (pActionID) {
        this.ClearDetailsCtrl()
        this.ClearResponseCtrl();
        this.SelectTabByIndex(1);
        this.LoadActionObject(pActionID);
    }
}